package com.example.p2pinvest.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.p2pinvest.R;
import com.example.p2pinvest.common.AppNetConfig;
import com.example.p2pinvest.common.BaseActivity;
import com.example.p2pinvest.util.MD5Utils;
import com.example.p2pinvest.util.UIUtils;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.nio.charset.StandardCharsets;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

public class UserRegisterActivity extends BaseActivity {


    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_title_setting)
    ImageView ivTitleSetting;
    @BindView(R.id.et_register_number)
    EditText etRegisterNumber;
    @BindView(R.id.et_register_name)
    EditText etRegisterName;
    @BindView(R.id.et_register_pwd)
    EditText etRegisterPwd;
    @BindView(R.id.et_register_pwdagain)
    EditText etRegisterPwdagain;
    @BindView(R.id.btn_register)
    Button btnRegister;

    @Override
    protected void initData() {

    }

    @Override
    protected void initTitle() {
        ivTitleBack.setVisibility(View.VISIBLE);
        tvTitle.setText("用户注册");
        ivTitleSetting.setVisibility(View.INVISIBLE);
    }

    @OnClick(R.id.iv_title_back)
    public void back(View view) {
        this.removeCurrentActivity();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_register;
    }

    @OnClick(R.id.btn_register)
    public void register(View view) {
        //获取用户注册信息
        String name = etRegisterName.getText().toString().trim();
        String number = etRegisterNumber.getText().toString().trim();
        String pwd = etRegisterPwd.getText().toString().trim();
        String pwdAgain = etRegisterPwdagain.getText().toString().trim();

        //1.所填写的信息都不能为空
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(number) || TextUtils.isEmpty(pwdAgain) || TextUtils.isEmpty(pwd)) {
            UIUtils.toast("所填写的信息都不能为空!!!", false);
        } else if (!pwd.equals(pwdAgain)) {
            //2.密码前后必须一致
            UIUtils.toast("密码前后必须一致!!!", false);
            etRegisterPwd.setText("");
            etRegisterPwdagain.setText("");
        } else {
            //联网上传注册信息
            RequestParams params = new RequestParams();
            params.add("name", name);
            params.add("phone", number);
            params.add("password", MD5Utils.MD5(pwd));
            client.post(AppNetConfig.USERREGISTER, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    String response = new String(responseBody, StandardCharsets.UTF_8);
                    JSONObject jsonObject = JSON.parseObject(response);
                    Boolean isExist = jsonObject.getBoolean("isExist");
                    if (isExist) {
                        //已经注册过
                        UIUtils.toast("用户已注册!!", false);
                    } else {
                        UIUtils.toast("注册成功", false);
                        UserRegisterActivity.this.removeCurrentActivity();
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    UIUtils.toast("联网请求失败", false);
                }
            });
        }
    }


}